﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;

namespace WPSPropertyPlugin
{
  public static class Extensions
  {
    public static string FromBase64(this string exp)
    {
      return Encoding.UTF8.GetString(Convert.FromBase64String(exp));
    }

    public static string ToBase64(this string exp)
    {
      if (string.IsNullOrEmpty(exp))
      {
        return string.Empty;
      }
      return Convert.ToBase64String(Encoding.UTF8.GetBytes(exp));
    }

    public static string MD5(this string exp)
    {
      if (string.IsNullOrEmpty(exp))
      {
        return null;
      }
      var md5 = new MD5CryptoServiceProvider();
      var byteData = Encoding.UTF8.GetBytes(exp);
      byteData = md5.ComputeHash(byteData);
      var sb = new StringBuilder();
      foreach (byte b in byteData)
      {
        sb.Append(b.ToString("x2"));
      }
      return sb.ToString().ToUpper();
    }

    public static string FormatJSON(this string exp)
    {
      if (string.IsNullOrEmpty(exp))
      {
        return string.Empty;
      }
      var serializer = new JsonSerializer();
      var tr = new StringReader(exp);
      var jtr = new JsonTextReader(tr);
      var obj = serializer.Deserialize(jtr);
      if (obj != null)
      {
        var textWriter = new StringWriter();
        var jsonWriter = new JsonTextWriter(textWriter)
        {
          Formatting = Formatting.Indented,
          Indentation = 4,
          IndentChar = ' '
        };
        serializer.Serialize(jsonWriter, obj);
        return textWriter.ToString();
      }
      else
      {
        return exp;
      }
    }

    public static string ToJSON(this object obj)
    {
      if (obj == null)
      {
        return "null";
      }
      return JsonConvert.SerializeObject(obj, Formatting.Indented);
    }

    public static IDictionary<string, object> ToDictionary(this string exp)
    {
      if (string.IsNullOrEmpty(exp))
      {
        return new Dictionary<string, object>();
      }
      return JsonConvert.DeserializeObject<Dictionary<string, object>>(exp);
    }

    public static string LeftSubstring(this string exp, char tagChar, string defaultVal = "")
    {
      var idx = exp.IndexOf(tagChar);
      if (idx == -1)
      {
        return defaultVal;
      }
      return exp.Substring(0, idx);
    }

    public static string RightEndString(this string exp, string tagString, string defaultVal = "")
    {
      var idx = exp.LastIndexOf(tagString);
      if (idx == -1)
      {
        return defaultVal;
      }
      return exp.Substring(idx + tagString.Length);
    }

    public static string MidSubstring(this string exp, char startChar, char endChar, string defaultVal = "")
    {
      var startIdx = exp.IndexOf(startChar);
      var endIdx = exp.LastIndexOf(endChar);
      if (endIdx <= startIdx)
      {
        return defaultVal;
      }
      return exp.Substring(startIdx + 1, endIdx - startIdx - 1);
    }

    public static T ReadJSON<T>(this string file)
    {
      if (File.Exists(file))
      {
        var content = File.ReadAllText(file);
        if (!string.IsNullOrEmpty(content))
        {
          return JsonConvert.DeserializeObject<T>(content);
        }
      }
      return default(T);
    }

    public static string ToString(this IDictionary obj, object key, string defaultValue = null)
    {
      object value;
      if ((obj == null || key == null) || !obj.Contains(key))
      {
        return defaultValue;
      }
      value = obj[key];
      if (value == null)
      {
        return defaultValue;
      }
      return value.ToString();
    }

    public static string ToJSON(this IDictionary obj, object key)
    {
      if ((obj == null || key == null) || !obj.Contains(key))
      {
        return null;
      }
      return obj[key].ToJSON();
    }

    public static string AddNameIndex(this string name, int increment = 1)
    {
      var nameArr = name.Split('_');
      var namePrefix = nameArr[0];
      var nameIdx = nameArr.Length > 1 ? int.Parse(nameArr[1]) + increment : 1;
      return $"{namePrefix}_{nameIdx.ToString("00")}";
    }
  }
}
